/**
 * @author jeroen
 */
package server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Statement;

public class DatabaseImpl {
	private Connection database;
	
	// database gegevens voor mysql
	private String host = "localhost";
	private String db = "cijferlijst";
	private String user = "root";
	private String pass = "";
	
	
	
	static {
		try{
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		}catch (Exception e){
			System.out.println("databaseconnectie mislukt");
		}
	}
	/**
	 * Constructor maakt een connectie aan en stored die in het object
	 * @param host String
	 * @param db String
	 * @param user String
	 * @param pass String
	 */
	public DatabaseImpl(){
		String url = "jdbc:mysql://" + host + "/" + db + "?" + "user=" + user + "&password=" + pass;
		//DriverManager.getConnection("jdbc:mysql://192.168.2.4/test?user=me&password="+pw)
		System.out.println("\nDatabaseImpl - Verbinding met database wordt gemaakt met: " + url);
		
		try {
		    //Connection conn = DriverManager.getConnection(url);
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/cijferlijst", user, pass);
			database = conn;
		    System.out.println(database);
		} catch (SQLException ex) {
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		}
	}
	/**
	 * Voert een query uit op de database en geeft de resultset terug
	 * @param query
	 * @return
	 */
	public ResultSet executeQuery(String query){
		Statement stmt;
		try {
			stmt = database.createStatement();
			if (stmt.execute(query)){
				System.out.println("DatabaseImpl - Return resultset");
				ResultSet rs = stmt.getResultSet();
				return rs;
			}
		}catch (SQLException sqle){
			System.out.println("DatabaseImpl - SQLException : "+ sqle.getMessage());
		}
		return null;
	}
	/**
	 * wat is hier de bedoeling mee? (jeroen)
	 */
	public void updateQuery(){}
	
	
}
